/*******************************************************************************

This code file produces Table A10, "Developer Participation Responses to Variation in 421-a Incentive: Simulated Instruments GMM Results."

*******************************************************************************/

*** Manage settings

	run "$dir/code/modules/settings.do"
	
*** Load Stata-TeX

	do "$code/modules/stata-tex.do"
	cd "$tables/estimates"

*** Prepare simulated instrument

	use "$data/raw/matched_buildings.dta", clear
	gcollapse (mean) dtaxrate_onsite, by(bbl)
	rename dtaxrate_onsite dtaxrate_onsite_sim
	
	tempfile sim
	save `sim', replace
	
*** Load data

	use "$data/clean/cleaned_data.dta", clear
	
	merge 1:1 bbl using `sim', nogen keep(1 3)
	
	gen constant = 1
	
********************************************************************************
* Complete table analysis
********************************************************************************
	
*** Logit, No FE

	gmm (inclusionary_onsite - invlogit({xb:dtaxrate_onsite constant})),  instruments(dtaxrate_onsite_sim) vce(cluster nta)
	store_est_tpl using tableA10.csv, coef(dtaxrate_onsite) name(logit0) all
	
	nlcom (sd: 3.14159/(sqrt(3)*_b[dtaxrate_onsite])), post
	store_est_tpl using tableA10.csv, coef(sd) name(sd0) all
	
*** Logit, borough FEs, expanding controls

	* No controls
	
	gmm (inclusionary_onsite - invlogit({xb:dtaxrate_onsite i.borough i.yearpermit constant})), ///
		instruments(dtaxrate_onsite_sim i.borough i.yearpermit) vce(cluster nta)
	store_est_tpl using tableA10.csv, coef(dtaxrate_onsite) name(logit1) all
	
	nlcom (sd: 3.14159/(sqrt(3)*_b[dtaxrate_onsite])), post
	store_est_tpl using tableA10.csv, coef(sd) name(sd1) all
	
	* Lot controls
	
	gmm (inclusionary_onsite - invlogit({xb:dtaxrate_onsite $controls_lot i.borough i.yearpermit constant})), ///
		instruments(dtaxrate_onsite_sim $controls_lot i.borough i.yearpermit) vce(cluster nta)
	store_est_tpl using tableA10.csv, coef(dtaxrate_onsite) name(logit2) all
	
	nlcom (sd: 3.14159/(sqrt(3)*_b[dtaxrate_onsite])), post
	store_est_tpl using tableA10.csv, coef(sd) name(sd2) all
	
	* Lot + neighborhood controls
	
	logit inclusionary_onsite dtaxrate_onsite $controls_lot $controls_block i.borough i.yearpermit, cl(nta) asis
	mat b_init = e(b)
			
	gmm (inclusionary_onsite - invlogit({xb:dtaxrate_onsite $controls_lot $controls_block i.borough i.yearpermit constant})), ///
		instruments(dtaxrate_onsite_sim $controls_lot $controls_block i.borough i.yearpermit) vce(cluster nta) ///
		conv_maxiter(10) from(b_init) quickderivatives
		
	nlcom (sd: 3.14159/(sqrt(3)*_b[dtaxrate_onsite])), post
	store_est_tpl using tableA10.csv, coef(sd) name(sd3) all

********************************************************************************
* Create table
********************************************************************************
	
	cat tableA10.csv

	cap erase "$tables/tableA10.tex"

	cd "$code/modules"
	
	capture table_from_tpl, t("$tables/templates/tableA10.tex") ///
						r("$tables/estimates/tableA10.csv") ///
						o("$tables/output/tableA10.tex") 
						
	capture table_from_tpl, t("$tables/templates/tableA10.tex") ///
						r("$tables/estimates/tableA10.csv") ///
						o("$tables_overleaf/tableA10.tex") 
		
		
